<?php

namespace App\Http\Resources\CruiseCheck;

use App\Http\Resources\BaseResource;
use App\Http\Resources\Cruise\CruiseTemplateResource;
use App\Models\Cruise\CruiseCheckSelfDetailModel;
use App\Models\Cruise\CruiseCheckSelfTaskCompanyMappingModel;
use App\Models\Cruise\CruiseTemplateModel;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;

class CruiseCheckSelfDetailResource extends BaseResource
{
    /**
     * Transform the resource into an array.
     *
     * @param Request $request
     * @return array
     */
    public function toArray($request)
    {
        $notProduceCount          = 0;
        $cruiseCheckSelfDetailRel = $this->cruiseCheckSelfTaskCompanyMappingsRel->map(function (
            CruiseCheckSelfTaskCompanyMappingModel $cruiseCheckSelfTaskCompanyMappingModel
        ) use (&$notProduceCount) {
            $taskCompanyNotProduceCount = $cruiseCheckSelfTaskCompanyMappingModel->cruiseCheckSelfDetailRel->where("status",
                CruiseCheckSelfDetailModel::STATUS_NOT_PRODUCE)->count();
            $notProduceCount            = $notProduceCount + $taskCompanyNotProduceCount;

            if ($this->templateType == CruiseTemplateModel::TYPE_5) {
                return [
                    "name"                 => attributeGet($cruiseCheckSelfTaskCompanyMappingModel,
                        "cruiseCheckCompanyRel.fsCompanyRel.name", "-"),
                    "cruiseCheckCompanyId" => attributeGet($cruiseCheckSelfTaskCompanyMappingModel,
                        "cruiseCheckCompanyRel.id", "0"),
                    "notProduceCount"      => $taskCompanyNotProduceCount,
                    "list"                 => CruiseCheckSelfDetailListResource::collection(
                        $cruiseCheckSelfTaskCompanyMappingModel->cruiseCheckSelfDetailRel->where("status", "<>",
                            CruiseCheckSelfDetailModel::STATUS_NOT_PRODUCE)
                    )
                ];
            }

            if ($this->templateType == CruiseTemplateModel::TYPE_8) {
                return [
                    "name"                 => attributeGet($cruiseCheckSelfTaskCompanyMappingModel,
                        "cruiseCheckShopRel.name", "-"),
                    "cruiseCheckCompanyId" =>  attributeGet($cruiseCheckSelfTaskCompanyMappingModel, "shopId", "-"),
                    "notProduceCount"      => $taskCompanyNotProduceCount,
                    "list"                 => CruiseCheckSelfDetailListResource::collection(
                        $cruiseCheckSelfTaskCompanyMappingModel->cruiseCheckSelfDetailRel->where("status", "<>",
                            CruiseCheckSelfDetailModel::STATUS_NOT_PRODUCE)
                    )
                ];
            }

            return [];
        });
        return [
            "name"                     => $this->name,
            "taskStartTime"            => formatDateYmd($this->taskStartTime),
            "taskEndTime"              => formatDateYmd($this->taskEndTime),
            "templateId"               => $this->templateId,
            "templateDetail"           => $this->resourceLoaded("cruiseTemplateRel") ? CruiseTemplateResource::collection([$this->cruiseTemplateRel])
                                                                                                             ->getIterator()
                                                                                                             ->current() : null,
            "taskCount"                => (($this->taskEndTime - $this->taskStartTime) / (24 * 60 * 60)) + 1 - $this->cruiseCheckSelfDetailRel->where("status",
                    "=", CruiseCheckSelfDetailModel::STATUS_NOT_PRODUCE)->count(),
            "cruiseCheckSelfDetailRel" => $cruiseCheckSelfDetailRel

//                $this->when(
//                    $this->cruiseCheckSelfDetailRel,
//                    $this->cruiseCheckSelfDetailRel
//                        ->where("status", "<>", CruiseCheckSelfDetailModel::STATUS_NOT_PRODUCE)
//                        ->groupBy("cruiseCheckCompanyId")
//                        ->map(function (Collection $cruiseCheckSelfDetailRelCollection) {
//                            return [
//                                "name"                 => $cruiseCheckSelfDetailRelCollection->offsetGet(0)->cruiseCheckCompanyRel->fsCompanyRel->name,
//                                "cruiseCheckCompanyId" => $cruiseCheckSelfDetailRelCollection->offsetGet(0)->cruiseCheckCompanyRel->id,
//                                "list"                 => CruiseCheckSelfDetailListResource::collection($cruiseCheckSelfDetailRelCollection)
//                            ];
//                        })->values(),
//                    []
//                )

        ];
    }
}
